﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_Parents : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IParentBLL BLL = new ParentBLL();
		private Main main = new Main();

        private RadComboBox cmbPrefixes;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmbPrefixes = (RadComboBox)cbPrefixes.FindControl("cbPrefixes");
            lblEmpSeq.Text = Session["emp_seq"].ToString();

            if (DataItem != null)
            {
                cbPrefixes.ListPrefixesComboBox();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    cmbParentType.SelectedValue = DataBinder.Eval(DataItem, "Type").ToString();
                    cmbPrefixes.SelectedValue = DataBinder.Eval(DataItem, "Person.NamePrefix.Sequence").ToString();
                    txtName.Text = DataBinder.Eval(DataItem, "Person.FirstName").ToString();
                    txtLastName.Text = DataBinder.Eval(DataItem, "Person.LastName").ToString();
                    txtCitizen.Text = DataBinder.Eval(DataItem, "Person.Citizen").ToString();
                    txtBirthDate.SetValue(DataBinder.Eval(DataItem, "Person.BirthDate", "{0:dd/MM/yyyy}").ToString());
                    rblParentStatus.SelectedValue = DataBinder.Eval(DataItem, "Status").ToString().Length > 0 ?
                        DataBinder.Eval(DataItem, "Status").ToString() : "1";
                    rblCareStatus.SelectedValue = Convert.ToBoolean(DataBinder.Eval(DataItem, "CareStatus")) ? "1" : "0";
                }
            }
        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            BLL.Insert(lblEmpSeq.Text.Trim()
                , cmbPrefixes.SelectedValue
                , txtName.Text.Trim()
                , txtLastName.Text.Trim()
                , txtCitizen.Text.Trim()
                , txtBirthDate.GetStringValue().Length > 0 ? DateTime.Parse(txtBirthDate.GetStringValue()) : (DateTime?)null
                , cmbParentType.SelectedValue
                , rblParentStatus.SelectedValue
                , rblCareStatus.SelectedValue == "1"
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            BLL.Update(Int32.Parse(lblSequence.Text.Trim())
                , lblEmpSeq.Text.Trim()
                , cmbPrefixes.SelectedValue
                , txtName.Text.Trim()
                , txtLastName.Text.Trim()
                , txtCitizen.Text.Trim()
                , txtBirthDate.GetStringValue().Length > 0 ? DateTime.Parse(txtBirthDate.GetStringValue()) : (DateTime?)null
                , cmbParentType.SelectedValue
                , rblParentStatus.SelectedValue
                , rblCareStatus.SelectedValue == "1"
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

    }
}